package com.qianfeng.teachdemo.ui.pulltorefresh;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.qianfeng.teachdemo.R;
import com.qianfeng.teachdemo.ui.BaseActivity;


import java.util.ArrayList;
import java.util.List;

/**
 *
 * 第三天上
 *
 * PullTorRefreshLibrary
 * 一.简介 pulltorefresh是github上十大受欢迎的开源库之一 是一套实现的非常好的下拉刷新库他支持
 * 1.ListView
 * 2.GridView
 * 3.SccollView;
 * 4.WebView
 * 5.多种常用的View
 * 他实用简单方便,扩展性强
 * 二 使用
 */

public class PullToRefreshActivity extends BaseActivity implements PullToRefreshBase.OnRefreshListener2<ListView>, AdapterView.OnItemClickListener {
    private PullToRefreshListView ptrListView;
    private RequestQueue queue;
    private int page = 1;
    private List<String> list = new ArrayList<String>();
    private ArrayAdapter<String> adapter;
    private ILoadingLayout startLayout;
    ILoadingLayout endLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pulltorefresh);
        initView();
    }

    private void initView() {
        /** 创建请求队列 */
        queue = Volley.newRequestQueue(this);
        //初始化控件
        ptrListView = (PullToRefreshListView) findViewById(R.id.pullToRefresh);
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        ptrListView.setOnItemClickListener(this);
        //初始化ptrListView加载控件
        ptrListView.setOnRefreshListener(this);
        startLayout = ptrListView.getLoadingLayoutProxy(true, false);
        endLayout = ptrListView.getLoadingLayoutProxy(false, true);
        initLoadingLayout();
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
        ptrListView.setAdapter(adapter);
        ptrListView.setRefreshing(false);

    }

    private void initLoadingLayout() {
        startLayout.setPullLabel("下拉刷新中ing");
        startLayout.setRefreshingLabel("正在下拉刷新中");
        startLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.ic_launcher));
        startLayout.setReleaseLabel("呵呵");

        endLayout.setRefreshingLabel("上拉加载更多");
        endLayout.setLoadingDrawable(getResources().getDrawable(R.drawable.ic_comment_weixin));
        endLayout.setReleaseLabel("哈哈!!!!!");
        endLayout.setRefreshingLabel("正在上拉更多中...");
    }


    public void loadData() {
        StringRequest request = new StringRequest(Request.Method.GET, "", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }

    /**
     * 下拉刷新 上啦加载更多
     */
    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        page = 1;
        String label = DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE);
        startLayout.setLastUpdatedLabel(label);
        new MyAsyncTask().execute();
    }

    /** */
    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        String label = DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE);
        endLayout.setLastUpdatedLabel(label);
        new MyAsyncTask().execute();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    private class MyAsyncTask extends AsyncTask<Void, Void, List<String>> {

        @Override
        protected List<String> doInBackground(Void... voids) {

            List<String> list = new ArrayList<String>();
            for (int i = 1; i < 21; i++) {
                list.add(new StringBuffer().append("第").append(page).append("页---->>").append("测试数据").append(i).toString());
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            page++;
            return list;
        }

        @Override
        protected void onPostExecute(List<String> result) {
            if (page == 1 && !list.isEmpty()) {
                list.clear();
            }
            list.addAll(result);
            adapter.notifyDataSetChanged();
            ptrListView.onRefreshComplete();
        }
    }


}
